﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADNColorPicker
{
	public partial class ColorPreview : UserControl
	{
		private Bitmap memBmp;
		private Color backColor = Color.White;

		public ColorPreview()
		{
			InitializeComponent();
		}

		public override Color BackColor
		{
			get { return backColor; }
			set
			{
				backColor = value;
				DrawPreview();
				this.Invalidate();
			}
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			e.Graphics.DrawImage(memBmp, 0, 0);
			base.OnPaint(e);
		}

		protected override void OnResize(EventArgs e)
		{
			CreateBitmap(this.ClientRectangle.Size);
			DrawPreview();
			base.OnResize(e);
		}

		private void CreateBitmap(Size s)
		{
			if (memBmp != null)
			{
				memBmp.Dispose();
				memBmp = null;
			}

			if (s.Height > 0 && s.Width > 0)
			{
				memBmp = new Bitmap(s.Width, s.Height);
			}
		}

		private void DrawPreview()
		{
			if (memBmp != null)
			{
				Graphics g = Graphics.FromImage(memBmp);
				g.Clear(Color.White);

				g.FillRectangle(new HatchBrush(HatchStyle.LargeCheckerBoard, Color.LightGray, Color.DarkGray),
					0, 0, memBmp.Width, memBmp.Height);

				g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, memBmp.Width, memBmp.Height));
			}
		}
	}
}
